<?php
/**
 * User: b179a9
 * Date: 9/20/11
 * Time: 6:21 PM
 */

class App_Form_Page_Form extends Zend_Form
{

    function init()
    {

        $this->setMethod('post');
        $this->setAction('');

        $id = new Zend_Form_Element_Hidden('id');
        $this->addElement($id);

        $title = new Zend_Form_Element_Text('title');
        $title->setLabel('Title:')
                ->setRequired()
                ->addValidators(array(new Zend_Validate_StringLength(1, 255)));
        $this->addElement($title);


        $content = new Zend_Form_Element_Textarea('content');
        $content->setLabel('Content:')
                ->setRequired();
        $this->addElement($content);

        $menu = new Zend_Form_Element_Checkbox('menu');
        $menu->setLabel('Should be shown in menu');
        $this->addElement($menu);


        // Родительская страница
        $parent = new Zend_Form_Element_Select('parent');

        $pagesManager = new \Managers\Pages();
        $pages = $pagesManager->getList(null, true);

        /**
         * Добавляем пустой эелемент (для корневых страниц)
         */
        $parent->addMultiOption('', 'None');
        foreach ($pages as $record) {
            $parent->addMultiOption($record['id'], $record['title']);
        }
        $parent->setLabel('Parent page if has');
        $parent->setRegisterInArrayValidator(false);
        $parent->addValidators(array(new Lib_Form_Validate_DbHasRecord('\Catalog\Pages', 'id')));
        $parent->addErrorMessage('Please, select valid parent page.');
        $this->addElement($parent);
        // Конец Родительская страница
        
        $sort = new Zend_Form_Element_Text('sortOrder');
        $sort->setRequired()
                ->setLabel('Sort order:')
                ->addValidators(array(new Zend_Validate_Digits()));
        $this->addElement($sort);

        $url = new Zend_Form_Element_Text('url');
        $url->setLabel('Page url:');
        $this->addElement($url);

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');

        $this->addElement($submit);

    }

}
